import { request, USE_MOCK } from './request'

export interface WithdrawMessage {
  id: number
  phone: string // ✅ 手机号（替换 merchantId）
  merchantName: string
  amount: number
  createTime: string
  status: 'pending' | 'paid'

  // ✅ 新的账户信息结构
  receiver: string // 收款人
  bankAccount: string // 银行账号
  bankName?: string // 开户行（可选）
}

/* ---------------- Mock 数据 ---------------- */
let mockWithdrawList: WithdrawMessage[] = [
  {
    id: 101,
    phone: '13800001111',
    merchantName: '张三水果店',
    receiver: '张三',
    bankAccount: '6214 **** 3321',
    bankName: '招商银行',
    amount: 320.5,
    createTime: '2025-01-10 14:20:00',
    status: 'pending'
  },
  {
    id: 102,
    phone: '13666668888',
    merchantName: '李四奶茶铺',
    receiver: '李四',
    bankAccount: '6214 **** 7895',
    bankName: '河北银行',
    amount: 150,
    createTime: '2025-01-11 09:30:00',
    status: 'paid'
  }
]

/** ✅ 获取提现列表 */
export async function getWithdrawMessages(): Promise<WithdrawMessage[]> {
  if (USE_MOCK) {
    const data = JSON.parse(JSON.stringify(mockWithdrawList)) as WithdrawMessage[]
    return request({ url: '/cash/list', method: 'GET' }, data)
  }
  return request({
    url: '/cash/list',
    method: 'GET'
  })
}

/** ✅ 更新提现状态为已打款 */
export async function updateWithdrawStatus(id: number): Promise<boolean> {
  if (USE_MOCK) {
    mockWithdrawList = mockWithdrawList.map(item =>
      item.id === id ? { ...item, status: 'paid' } : item
    )
    return request({ url: `/cash/update/${id}`, method: 'POST' }, true)
  }
  return request({
    url: `/cash/update/${id}`,
    method: 'POST'
  })
}
